package com.lbd.media.controller;

import com.lbd.media.bean.FaqType;
import com.lbd.media.service.FaqTypeService;
import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.Dictionary;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/faqtype")
public class FaqTypeController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private FaqTypeService faqtypeService;

    /**
     * 按条件分页查询faqtype
     *
     * @param page    第几页
     * @param rows    一页多少条
     * @param faqtype
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, FaqType faqtype) {
        return new Result(ResultState.SUCCESS, "查询成功", faqtypeService.findByPageConditionJpql(page, rows, faqtype));
    }

    /**
     * 保存faqtype信息
     *
     * @param faqtype
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(FaqType faqtype) {
        if (faqtype.getParent() == null || StringUtils.isBlank(faqtype.getParent().getId())) {
            faqtype.setParent(null);
        }
        faqtypeService.save(faqtype);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 根据id查询回显要编辑的FaqType
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", faqtypeService.findById(id));
    }

    /**
     * 修改faqtype信息
     *
     * @param faqtype
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(FaqType faqtype) {
        if (faqtype.getParent() == null || StringUtils.isBlank(faqtype.getParent().getId())) {
            faqtype.setParent(null);
        }
        faqtypeService.update(faqtype);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        faqtypeService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功");

    }

    /**
     * 获取全部上层问题分类
     *
     * @return
     */
    @RequestMapping(value = "/parents", method = RequestMethod.GET)
    public Result getParents() {
        List<FaqType> parents = faqtypeService.findAllParents();

        return new Result(ResultState.SUCCESS, "查询成功", parents);
    }

    /**
     * 获取全部常见问题二层分类作为问题分类
     *
     * @return
     */
    @RequestMapping(value = "/types", method = RequestMethod.GET)
    public Result getFaqTypes() {
        List<FaqType> types = faqtypeService.findAllTypes();

        return new Result(ResultState.SUCCESS, "查询成功", types);
    }

}
